package com.boat.sample.ddd.infrastructure.persistence.factory;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Component;

import com.boat.sample.ddd.domain.system.entity.User;
import com.boat.sample.ddd.infrastructure.persistence.po.UserPo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.extern.slf4j.Slf4j;

/**
 * User工厂类
 * @author noia_zhou
 *
 */
@Slf4j
@Component
public class UserFactory {
	public User buildUser(UserPo userPo) {
		if (userPo == null) {
			return null;
		}

		User user = new User();
		BeanUtil.copyProperties(userPo, user, CopyOptions.create().ignoreError());
		return user;
	}

	public UserPo buildUserPo(User user) {
		if (user == null) {
			return null;
		}

		UserPo userPo = new UserPo();
		BeanUtil.copyProperties(user, userPo, CopyOptions.create().ignoreError());
		return userPo;
	}

	public List<UserPo> buildUserPoList(Collection<User> userList) {
		if (userList == null) {
			return null;
		}
		
		return userList.stream().map(user -> buildUserPo(user)).collect(Collectors.toList());
	}

	public List<User> buildUserList(Collection<UserPo> poList) {
		if (poList == null) {
			return null;
		}
		
		return poList.stream().map(userPo -> buildUser(userPo)).collect(Collectors.toList());
	}
    
	public UserPo buildDeleteUserPo(String id) {
		if (id == null) {
			return null;
		}

		UserPo userPo = new UserPo();
		userPo.setId(id);
		userPo.setDeleted(Long.valueOf(System.currentTimeMillis()/1000).intValue());
		return userPo;
	}
}
